#encoding:utf-8
'''
Created on 2012/3/5
argv[1]為正確的Label
argv[2]為class的weight
argv[3]為結果
@author: Netdb_Heng
'''
#參數一為母體，參數二為輸入Label，參數三為輸出sample，參數四為輸出sample的class，參數五為筆數

import sys

print ( "compare:" + sys.argv[1] + "and" + sys.argv[2] + "output to " + sys.argv[3] )

Label_inf = open(sys.argv[1])
Predict_inf = open(sys.argv[2])

outf = open(sys.argv[3],'w')
BestFscore_outf = open(sys.argv[4],'w')
negative = sys.argv[5]

Label_lines = Label_inf.readlines()
Predict_lines = Predict_inf.readlines()

instance=0
Max_number = 0 #Fscore最高的為哪一組


def Fscore(Class,Predict):
    same=0
    for i in range(len(Class)):
        for j in range(len(Predict)):
            if(int(Class[i]) == int(Predict[j]) ):
                same+=1
                break
    Precision = float(same) / len(Predict)
    Recall = float(same) / len(Class)
    if(Recall==0 and Precision==0):
        return 0
    else:
        return float(2*Precision*Recall) / (Precision+Recall)
    
def threshold(weight_A,weight_B):
    return str(float(weight_B)/weight_A) 

outf.write("ID,Max_Fscore_Predict,Max_Fscore,Max_Fscore_threshold,Max_Fscore_threshold_after,Max_Fscore_threshold_choose, ,")

for i in range(26):
    if i==0:
        outf.write("Predict,Fscore,threshold,threshold_after,threshold_choose")
    else:
        outf.write(",Predict,Fscore,threshold,threshold_after,threshold_choose")
     
outf.write("\n")    

for i in range(len(Predict_lines)):
    outf.write(str(i+1) + ",")  #ID    
    Predict = Predict_lines[i].split(",")
    Label = Label_lines[i].split(",")
    Predict_part_comp = []
    Fscore_comp = []
    threshold_comp = []
    threshold_after_comp = []
    threshold_choose_comp = []
    
    Predict_part = [] #每種猜測結果
    #Max_Predict = 0
    #Max_Fscore = 0
    for j in range(len(Predict)):   #儲存結果用
        
        Predict_part_inter = []
        
        if float(Predict[j].split("=")[1])<0:
            break
        '''
        if(j>0):
            outf.write(",")
        '''
        for k in range(j+1):
            if(k==0):
                Predict_part_print = str(int(Predict[k].split("=")[0]))
                #outf.write(str(int(Predict[k].split("=")[0])) )
                Predict_part_inter.append(Predict[k].split("=")[0])
            else:
                Predict_part_print += "_" + str(int(Predict[k].split("=")[0]))
                #outf.write("_" + str(int(Predict[k].split("=")[0])))
                Predict_part_inter.append(Predict[k].split("=")[0])
        Predict_part.append(Predict_part_inter)
        Predict_part_comp.append(Predict_part_print)#把每一個猜測的結果記錄下來
        Fscore_comp.append(Fscore(Label,Predict_part[j]))
        
        if(float(threshold(float(Predict[0].split("=")[1]),float(Predict[j].split("=")[1]))) == 1):
            threshold_choose_comp.append(threshold(float(Predict[0].split("=")[1]),float(Predict[j+1].split("=")[1])))
        else:
            threshold_choose_comp.append(threshold(float(Predict[0].split("=")[1]),float(Predict[j].split("=")[1])))
            
        threshold_comp.append(threshold(float(Predict[0].split("=")[1]),float(Predict[j].split("=")[1])))
        #if(j+1<len(Predict))
        if(int(negative) == 1):
            if(float(Predict[j+1].split("=")[1])<0):
                threshold_after_comp.append(0.0)
            else:
                threshold_after_comp.append(threshold(float(Predict[0].split("=")[1]),float(Predict[j+1].split("=")[1])))
        else:
            if(j==len(Predict)-1):
                threshold_after_comp.append(threshold(float(Predict[0].split("=")[1]),float(Predict[j].split("=")[1])))
            else:
                threshold_after_comp.append(threshold(float(Predict[0].split("=")[1]),float(Predict[j+1].split("=")[1])))
    for j in range(len(Predict_part_comp)):#比較出最大的Fscore
        if j==0:
            Max_Predict = Predict_part_comp[j]
            Max_Fscore = Fscore_comp[j]
            Max_threshold = threshold_comp[j]
            Max_threshold_after = threshold_after_comp[j]
            Max_threshold_choose = threshold_choose_comp[j]
            Max_number = j
        else:
            if(Fscore_comp[j]>Max_Fscore):
                Max_Predict = Predict_part_comp[j]
                Max_Fscore = Fscore_comp[j]
                Max_threshold = threshold_comp[j]
                Max_threshold_after = threshold_after_comp[j]
                Max_threshold_choose = threshold_choose_comp[j]
                Max_number = j
    outf.write(Max_Predict + "," + str(Max_Fscore) + "," + str(Max_threshold) + "," + str(Max_threshold_after) + "," + str(Max_threshold_choose) + ", ,")
    for j in range(len(Predict_part[Max_number])):
        if j==0:
            BestFscore_outf.write(str(int(Predict_part[Max_number][j])))
        else:
            BestFscore_outf.write("," + str(int(Predict_part[Max_number][j])))
    BestFscore_outf.write("\n")
    
    for j in range(len(Predict_part_comp)):
        if j==0:
            outf.write(Predict_part_comp[j] + "," + str(Fscore_comp[j]) + "," + str(threshold_comp[j]) + "," + str(threshold_after_comp[j]) + "," + str(threshold_choose_comp[j]))
        else:
            outf.write("," + Predict_part_comp[j] + "," + str(Fscore_comp[j]) + "," + str(threshold_comp[j]) + "," + str(threshold_after_comp[j]) + "," + str(threshold_choose_comp[j]))
                 
        #outf.write("," + str() )#Fscore
        #outf.write(","   )#threshold
    outf.write("\n")
       
Label_inf.close();
Predict_inf.close();
outf.close();

